package com.six.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.bean.Product;
import com.six.service.ProductService;
import com.six.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("product")
public class ProducrController {
    @Autowired
    ProductService productService;

    @GetMapping("list")  //获取不用requestmapping
    public Result getList(){
        List<Product> list=productService.list();
        return Result.success(list);
    }

    @PostMapping
    public Result add(@RequestBody Product product){
        productService.save(product);
        Product product1=productService.getById(product.getPid());
        return Result.success("保存成功",product); //返回添加成功的tadmin
    }
    @DeleteMapping("/{id}")  // 1
    public Result delete(@PathVariable("id") int pid){ // 1
        boolean b=productService.removeById(pid);
        if(b){
            return Result.success("删除成功");
        }
        else{
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Product product){
        boolean b=productService.saveOrUpdate(product);
        Product product1=productService.getById(product.getPid());
        if(b){
            return Result.success("保存成功",product);
        }
        else{
            return Result.fail(201,"修改失败");
        }
    }
    @GetMapping("page")
    public Result getpage(Page page){ //获取分页数据
        Page result=productService.page(page); //返回分页数据
        return Result.success(result);
    }
}
